﻿Namespace helper
    Public NotInheritable Class HexHelper


        ''' <summary>
        ''' Encode from Alphadecimal to Hexadecimal
        ''' </summary>
        ''' <param name="Alphadecimal">Alphadecimal</param>
        ''' <returns>Hexadecimal</returns>
        ''' <remarks>Encode from Alphadecimal to Hexadecimal</remarks>
        Public Shared Function Encode(ByVal Alphadecimal As String) As String
            'validation
            If String.IsNullOrEmpty(Alphadecimal) Then
                Throw New ArgumentNullException("Alphadecimal")
            End If


            Dim icount As Integer
            Dim pEncodeText As String = ""
            Dim tempHex As String

            For icount = 1 To Len(Alphadecimal)
                tempHex = Hex(AscW(Mid$(Alphadecimal, icount, 1)))
                pEncodeText = pEncodeText & tempHex.PadLeft(4, "0")
            Next

            Return pEncodeText
        End Function

        ''' <summary>
        ''' Decode from Hexadecimal to Unicode 
        ''' </summary>
        ''' <param name="Hexadecimal">Hexadecimal</param>
        ''' <returns>Unicode</returns>
        ''' <remarks></remarks>
        Public Shared Function DecodeToUnicode(ByVal Hexadecimal As String) As String

            If String.IsNullOrEmpty(Hexadecimal) Then
                Throw New ArgumentNullException("Hexadecimal")
            End If

            Dim icount As Integer
            Dim pDecodeText As String = ""

            For icount = 1 To Len(Hexadecimal) Step 4
                pDecodeText = pDecodeText & "&#x" & Mid(Hexadecimal, icount, 4) & ";"
                'pDecodeText = pDecodeText & Mid(pText, icount, 4)
            Next

            Return pDecodeText
        End Function


        ''' <summary>
        ''' See Whether the string is Hexadecimal
        ''' </summary>
        ''' <param name="value">The string to test.</param>
        ''' <returns>True if string is a hexadecimal number, False if otherwise.</returns>
        ''' <remarks>True if string is a hexadecimal number, False if otherwise.</remarks>
        Public Shared Function IsHex(ByVal value As String) As Boolean

            If IsNullOrWhitespace(value) Then _
                Return False

            Dim i As Int32, c As Char

            If value.IndexOf("0x") = 0 Then _
                value = value.Substring(2)

            While (i < value.Length)
                c = value.Chars(i)

                If Not (((c >= "0"c) AndAlso (c <= "9"c)) OrElse _
                        ((c >= "a"c) AndAlso (c <= "f"c)) OrElse _
                        ((c >= "A"c) AndAlso (c <= "F"c))) _
                Then
                    Return False
                Else
                    i += 1
                End If
            End While

            Return True
        End Function


        ''' <summary>
        ''' Indicates whether a specified string is Nothing, empty, or consists only of white-space characters.
        ''' </summary>
        ''' <param name="value">The string to test.</param>
        ''' <returns>true if the value parameter is Nothing or String.Empty, or if value consists exclusively of white-space characters. </returns>
        ''' <remarks></remarks>
        Private Shared Function IsNullOrWhitespace(ByVal value As String) As Boolean
            Dim rev As Boolean = False
            If value Is Nothing Then
                Return True
            End If


            For i = 0 To value.Length - 1
                If Char.IsWhiteSpace(value, i) Then
                    rev = True
                    Return rev
                End If
            Next


            Return rev
        End Function
    End Class
End Namespace

